#-*- coding: utf-8 -*-
# 动画类，中途可回调函数
from base import CObject
from sprite import CSprite
from object import *
from pyglet.gl import *
import ani.actions

class CAni(CObject,CSprite):
	m_SID=0
	m_Config={}
	m_Action={}

	def __init__(self,iAniID):
		self.m_ID	=iAniID
		sImg		=self.m_Config["img"]
		tAnchor		=self.m_Config.get("中心",(0,0))
		iOpacity	=self.m_Config.get("透明度",255)
		CSprite.__init__(self,sImg,anchor=tAnchor,opacity=iOpacity)
		CObject.__init__(self)

	def Play(self,parent,cbfunc=None):
		if cbfunc:
			self.m_Callback=cbfunc
		parent.add(self)
		self.DoStep(1)

	def DoStep(self,iStep):
		dStep=self.m_Step[iStep]
		print "DoStep",iStep,dStep
		ani.actions.OnAction(self,dStep)
		delay=dStep.get("时长",0)
		if not iStep+1 in self.m_Step:
			self.StepEnd()
			return
		if delay>0:
			self.Call_Out(delay,NextAniStep,self.m_ID,"DoStep",iStep+1)
		else:
			self.DoStep(iStep+1)

	def StepEnd(self):
		self.kill()
		DelAni(self.m_ID)
		print "StepEnd"

def NextAniStep(iAniID,sFunc,*args):
	aniobj=GetAni(iAniID)
	if not aniobj:
		return
	func=getattr(aniobj,sFunc,None)
	if not func:
		return
	func(*args)

Modules={}
import a0001
Modules[1]=a0001
